<template>
	<view class="container">
		<z-paging ref="paging" v-model="queryData" @query="queryList" :default-page-size="10">
			<template #top>
				<tn-navbar back-text="返回" > 货主充值查询 </tn-navbar>

				<!-- <tn-navbar height="45px" home-icon="" :bottom-shadow = "false" bg-color="tn-type-primary_bg"  :before-back="beforeBack">货主充值查询 </tn-navbar> -->

				<view class=" tn-p-sm tn-text-xl tn-white_bg" @click="openCalendarPopup">
					日期范围：{{dataInfo.startDate}} 至 {{dataInfo.endDate}}
				</view>
				<!-- 货主选择 -->
				<view class="owner-section tn-pl-sm tn-pb-sm tn-pr-sm">
					<picker @change="handleOwnerChange" :value="selectedIndex" :range="orgList" range-key="orgname">
						<view class="owner-picker tn-p-sm">
							<text class="owner-name">{{ selectedIndex !== -1 ? orgList[selectedIndex]?.orgname : '请选择下级' }}</text>
							<text class="tn-icon-down"></text>
						</view>
					</picker>
				</view>
			</template>

			<!-- 列表区域 -->
			<view class="list-container">
				<view 
					class="list-item"
					v-for="(item, index) in queryData" 
					:key="index"
					hover-class="item-hover"
					@click="clickRow(item)"
				>
					<view class="item-header">
						<text class="item-title">{{item.nextorgname}}</text>
						<text class="item-amount">{{item.taxamount}}</text>
					</view>
					<view class="item-content">
						<text class="item-desc">{{item.summaries}}</text>
						<text class="item-balance">{{item.ye || '0.00'}}</text>
					</view>
					<view class="item-footer">
						<text class="item-order">{{item.billcode || '--'}}</text>
						<text class="item-time">{{item.dates}} {{item.ontime}}</text>
					</view>
					<view class="item-remark" v-if="item.remark">
						<text class="remark-label">备注：</text>
						<text class="remark-content">{{item.remark}}</text>
					</view>
				</view>
			</view>
		</z-paging>
		
		<tn-popup v-model="showCalendarPopup" open-direction="top">
			<view class="tn-pt-xl tn-mt-xl">
				<tn-calendar v-model="selectDate" min-date="2024-01-01" mode="range" />
			</view> 
			<view class="tn-mt tn-w-full tn-pb">
				<tn-button
				font-size="36"
				custom-class="popup-calendar-button"
				@click="closeCalendarPopupPage"
				>
				选择所选日期
				</tn-button>
			</view>
		</tn-popup>
		
	</view>
</template>

<script setup>
	import { ref, reactive, onMounted } from 'vue'
	import { postCall } from '@/api/index.js'
	import dayjs from "dayjs";
	const procName = "PROC_APP_DO_ORG";
	import { createParams } from '@/utils/utils.js'
	const selectedIndex = ref(-1)
	const orgList = ref([])
	const head = reactive({
		orgName: "",
		startDate: "",
		endDate: ""
	});
	const dataInfo = reactive({"startDate":"2024-01-01","endDate":"2024-04-31"})
	const selectDate = ref('')
	const paging = ref(null);
	const queryData = ref([]);
	const beforeBack = () => {
		return new Promise((resolve) => {
			setTimeout(() => {
			uni.redirectTo({
				url: '/pages/index/index'
			});
			return true;
			}, 500)
		})
	}
	const showCalendarPopup = ref(false)
	const openCalendarPopup = () => {
	  showCalendarPopup.value = true
	}
	const closeCalendarPopupPage = () =>{
		showCalendarPopup.value = false;
		let date = {
						"startDate":dayjs(selectDate.value[0]).format('YYYY-MM-DD'),
						"endDate":dayjs(selectDate.value[1]).format('YYYY-MM-DD')
					}
		 Object.assign(dataInfo,date);
		 paging.value.reload();
	}
	const getNextOrgList = async () =>{
		let params = {};
		params["P_PROC_NAME"] = procName;
		params["P_ACTION"] = "GETNEXTORG";
		await postCall(params).then(res=>{
			if(res){
				if(res.code===200){
					orgList.value = [{orgname: '全部', orgid: ''}, ...res.data.DATASET];
					console.log(orgList.value);
				}
			}
		})
	}
	onMounted(() => {
		
		getNextOrgList();
		const firstDayOfMonth = dayjs().startOf('month');
		let date = {
						"startDate":firstDayOfMonth.format('YYYY-MM-DD'),
						"endDate":dayjs().format("YYYY-MM-DD")
					}
		Object.assign(dataInfo,date);
	})
	
	// 处理货主选择
	const handleOwnerChange = (e) => {
		selectedIndex.value = e.detail.value;
		paging.value.reload();
	}

	
	const queryList = (pageNo, pageSize) => {
		let params = {};
		params["P_ACTION"] = "RECHARGE_SELECT";
		params["P_PROC_NAME"] = procName;
		params["P_PAGE"] = pageNo;
		params["P_PAGESIZE"] = pageSize;
		params["P_NEXTORGID"] = orgList.value[selectedIndex.value]?.orgid ?? '';
		postCall(Object.assign(createParams(dataInfo), params)).then(res => {
			if (res) {
				paging.value.complete(res.data.DATASET);
			} 
		}).catch(() => {
			paging.value.complete(false);
		});
	}
	
	const clickRow = (item) => {
		// 处理行点击事件
		console.log('clicked row:', item);
	}
</script>

<style lang="scss">
.container {
	background-color: #f5f5f5;
	min-height: 100vh;
}

/* 导航栏样式 */
.nav {
	background-color: #ffffff;
	padding-top: var(--status-bar-height);
}

.nav-content {
	height: 44px;
	position: relative;
	display: flex;
	align-items: center;
	padding: 0 15px;
}

.nav-left {
	position: absolute;
	left: 15px;
}

.nav-back {
	color: #333;
	font-size: 16px;
}

.nav-title {
	width: 100%;
	text-align: center;
	font-size: 17px;
	color: #333;
	font-weight: 500;
}

/* 搜索区域样式 */
.search-box {
	background-color: #ffffff;
	padding: 15px;
	margin-bottom: 10px;
}

.input-group {
	margin-bottom: 15px;
}

.input-item {
	margin-bottom: 15px;
}

.input-label {
	font-size: 14px;
	color: #333;
	margin-bottom: 8px;
	display: block;
}

.uni-input {
	height: 40px;
	background-color: #f8f8f8;
	border-radius: 4px;
	padding: 0 12px;
	font-size: 14px;
}

.date-input {
	height: 40px;
	background-color: #f8f8f8;
	border-radius: 4px;
	padding: 0 12px;
	display: flex;
	align-items: center;
	font-size: 14px;
}

.search-btn {
	width: 100%;
	height: 44px;
	line-height: 44px;
	text-align: center;
	font-size: 16px;
	color: #fff;
	border-radius: 22px;
	background: linear-gradient(to right, #47e8be, #3379dc);
}

/* 列表样式 */
.list-container {
	padding: 15rpx;
}

.list-item {
	background-color: #ffffff;
	border-radius: 8rpx;
	padding: 30rpx;
	margin-bottom: 8rpx;
}

.item-hover {
	background-color: #f8f8f8;
}

.item-header {
	display: flex;
	justify-content: space-between;
	align-items: flex-start;
	margin-bottom: 8rpx;
}

.item-title {
	font-size: 28rpx;
	color: #333;
	font-weight: bold;
}

.amount-section {
	display: flex;
	flex-direction: row;
	align-items: center;
	gap: 16rpx;
}

.item-amount {
	font-size: 32rpx;
	color: #47e8be;
	font-weight: bold;
}

.item-balance {
	font-size: 24rpx;
	color: #666;
}

.item-content {
	display: flex;
	justify-content: space-between;
	align-items: center;
	margin-bottom: 8rpx;
}

.item-desc {
	font-size: 24rpx;
	color: #666;
	line-height: 1.3;
	flex: 1;
}

.item-balance {
	font-size: 24rpx;
	color: #666;
	margin-left: 16rpx;
	white-space: nowrap;
}

.item-footer {
	display: flex;
	justify-content: space-between;
	align-items: center;
}

.item-order {
	font-size: 22rpx;
	color: #999;
}

.item-time {
	font-size: 22rpx;
	color: #999;
}

/* 弹窗样式 */
.popup-content {
	background-color: #ffffff;
	border-radius: 12px 12px 0 0;
	padding: 15px;
}

.date-picker {
	height: 200px;
	width: 100%;
}

.picker-item {
	line-height: 50px;
	text-align: center;
}

.popup-buttons {
	display: flex;
	justify-content: space-between;
	margin-top: 15px;
}

.popup-btn {
	width: 48%;
	height: 44px;
	line-height: 44px;
	text-align: center;
	border-radius: 22px;
	font-size: 16px;
}

.confirm {
	background: linear-gradient(to right, #47e8be, #3379dc);
	color: #fff;
}

.owner-section {
	background-color: #fff;
	
	.owner-picker {
		background-color: #fff;
		border-radius: 12rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.05);
		
		.owner-name {
			font-size: 32rpx;
			color: #333;
		}
		
		.tn-icon-down {
			color: #999;
			font-size: 24rpx;
		}
	}
}

.search-section {
	padding: 0 30rpx 20rpx;
	
	:deep(.tn-search-box) {
		background-color: #fff;
		border-radius: 12rpx;
		box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.05);
		
		.tn-search-box__input {
			height: 72rpx;
			font-size: 28rpx;
		}
		
		.tn-search-box__icon {
			font-size: 32rpx;
		}
	}
}

.date-section {
	padding: 30rpx;
}

.date-picker-box {
	display: flex;
	justify-content: space-between;
	align-items: center;
}

.date-item {
	background-color: #fff;
	padding: 24rpx 30rpx;
	border-radius: 12rpx;
	display: flex;
	justify-content: space-between;
	align-items: center;
	box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.05);
}

.date-label {
	font-size: 32rpx;
	color: #333;
}

.date-value {
	display: flex;
	align-items: center;
}

.date-value text {
	font-size: 28rpx;
	color: #333;
	margin-left: 10rpx;
}

.tn-icon-calendar {
	font-size: 24rpx;
	color: #999;
}
.popup-calendar-button {
  margin: 0rpx 30rpx;
  width: calc(100% - 60rpx) !important;
  padding: 20rpx 30rpx !important;
}

.item-remark {
	margin-top: 8rpx;
	padding-top: 8rpx;
	border-top: 1rpx solid #f5f5f5;
	display: flex;
	align-items: flex-start;
}

.remark-label {
	font-size: 22rpx;
	color: #999;
	white-space: nowrap;
}

.remark-content {
	font-size: 22rpx;
	color: #666;
	flex: 1;
	word-break: break-all;
	line-height: 1.3;
}
</style> 